/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.api.administration;


import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.event.EventListenerList;

import fr.univ_tln.inf9.exaplanning.api.administration.event.ListTicketChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.administration.event.ListTicketListener;
import fr.univ_tln.inf9.exaplanning.api.administration.event.NameChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.administration.event.NameListener;
import fr.univ_tln.inf9.exaplanning.api.administration.event.TicketAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.administration.event.TicketRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.composante.Component;
import fr.univ_tln.inf9.exaplanning.api.db.AbstractEntity;
import fr.univ_tln.inf9.exaplanning.api.db.EntityInfo;
import fr.univ_tln.inf9.exaplanning.api.db.SessionManager;
import fr.univ_tln.inf9.exaplanning.api.salle.Room;


/**
 * @author pivi
 * @author emilien (persistance)
 */
@EntityInfo(	collections = { Ticket.class, Component.class },
				key = "email",
				methods = { "getEmail" },
				rows = { "email" },
				table = "admin",
				variables = { "" }) 
public class Administrator extends AbstractEntity {

	private final String email;
	private List<String> components;
	private List<String> ticketsAdmin = new LinkedList<String>();
	private EventListenerList listeners;
	
	
	public Administrator(String email) throws IOException, SQLException {
		super(true);
		this.email = email;
		listeners = new EventListenerList();
	}
	
	/**
	 * @param name
	 * @param ticketsAdmin
	 * @throws SQLException 
	 * @throws IOException 
	 */
	private Administrator(String email, List<String> ticketsAdmin, List<String> components) throws IOException, SQLException {
		super(false);
		this.email = email;
		this.ticketsAdmin = ticketsAdmin;
		this.components = components;
		listeners = new EventListenerList();
	}

	/**
	 * @return the name
	 */
	public String getEmail() {
		return email;
	}


	/**
	 * @return the ticketsAdmin
	 */
	public List<String> getTicketsAdmin() {
		return ticketsAdmin;
	}

	/**
	 * @param ticketsAdmin the ticketsAdmin to set
	 * fireListTickedChanged() prévient que la liste à changer
	 */
	public void setTicketsAdmin(List<String> ticketsAdmin) {
		this.ticketsAdmin = ticketsAdmin;
		fireListTicketChanged();
	}

	
	/**
	 * @param e
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 * * Le fireAddTicketToList() prévient qu'un ticket à été ajouté
	 */
	public boolean add(Ticket e) {
		fireAddTicketToList();
		return ticketsAdmin.add(e.getId());
	}


	

	/**
	 * @param o
	 * @return
	 * @see java.util.List#remove(java.lang.Object)
	 * Le fireRemoveTicketToList() prévient qu'un ticket à été supprimé
	 */
	public boolean remove(Object o) {
		fireRemoveTicketToList();
		return ticketsAdmin.remove(o);
	}

	public boolean add(Component arg0) {
		return components.add(arg0.getName());
	}

	public boolean contains(Component arg0) {
		return components.contains(arg0.getName());
	}

	public boolean isEmpty() {
		return components.isEmpty();
	}

	

	public String TicketsAdmintoString() {
		return "Administrator [ticketsAdmin=" + ticketsAdmin + "]";
	}

	
	/**
	 * Méthodes de l'api persistance, utile pour retrouver un élément grâce à sa clé
	 * 
	 * @param key
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	public Object findByKey(String key) throws IOException, SQLException {
    	List<List<String>> data = SessionManager.getInstance().getData(Administrator.class, key);
    	
    	List<String> tickets = data.get(1);
    	List<String> components = data.get(2);
    	
    	Administrator result = new Administrator(key, tickets, components);
    	
    	return result;
	}
	
	/****************************************************************************************************************/
	/****************************************			LISTENER				*************************************/
	 /*********************Cette partie est utile pour l'implentation du modèle MVC 			*********************/
	/****************************************************************************************************************/
	
	
	/**
	 * Gestion des listeners
	 */
	public void addListTicketListener(ListTicketListener listener){
		listeners.add(ListTicketListener.class, listener);
	}
	
	public void removeListTicketListener(ListTicketListener l){
		 listeners.remove(ListTicketListener.class, l);
	}
	
	
	
	/**
	 * Définition de tous les fires
	 */
	private void fireListTicketChanged() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.ListTicketChanged(new ListTicketChangedEvent(this, getTicketsAdmin()));
		}
		
	}

	private void fireAddTicketToList() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.TicketAddedToList(new TicketAddedToListEvent(this, TicketsAdmintoString()));
		}
		
	}
	
	
	private void fireRemoveTicketToList() {
		ListTicketListener[] listenerList = (ListTicketListener[])listeners.getListeners(ListTicketListener.class);
		
		for(ListTicketListener listener : listenerList){
			listener.TicketRemovedToList(new TicketRemovedToListEvent(this, TicketsAdmintoString()));
		}
		
	}
}
